// Javascript

// Performs operation on a number
function main() {
	var num = document.getElementById("frmInput").value;
	var opSelect = isValid(num);
	if (opSelect === 0)
		factor(num);
	else
		convert(num);
}
// Validates input
function isValid(num) {
	var op = document.getElementsByName("op");
	var opSelect = 0;
	try 
	{
		if (!(op[0].checked) && !(op[1].checked))
			throw "No operation selected";
		if (document.getElementById("frmInput").value === "")
			throw "Not a base10 whole number";
		if (num < 0)
			throw "Less than zero";
		if ((num % 1) !== 0)
            throw "Not a base10 whole number";
		if (op[1].checked)
			opSelect = 1;
		return opSelect;			
	}
    catch(err)
    {
		document.getElementsByName("frmInput").value = "";
		alert("Error: " + err + ".");
    }
}
// Gets number factors
function factor() {
	var number = document.getElementById("frmInput");
	var ans = document.getElementById("ans");
	var fCon = document.getElementById("fCon");
    var div = 0;
    var nums=[];
    var out = "";
    while (++div <= number.value) 
        if ((number.value % div) === 0)
            nums[nums.length] = div;
	var i = 0, j = nums.length - 1;
    if (fCon.checked)
		for (i; i <= j; i++)
			nums[i] = convert(nums[i]);
    i = 0;
    while (i <= j)
        out += "[<span id='spanAns'>" + nums[i++] + "</span>,<span id='spanAns'>" + nums[j--] + "</span>] ";
    ans.innerHTML = out;
}
// Performs number base conversion
function convert(num)
{
	var out = "";
	var base = getDropdown();
	var ans = document.getElementById("ans");
	var fCon = document.getElementById("fCon");
    while (num > 0)
	{
		if ((num % base) > 9)
			out = getAlpha(num, base) + out;
		else
			out = (num % base) + out;
		num = (num - (num % base)) / base;
	}
	if (fCon.checked)
		return out;
	else
		ans.innerHTML = out;
}
// Gets base for number conversion
function getDropdown() {
	var d = document.getElementById("dropdown");
	var sel = d.selectedIndex;
	var opt = d.options;
	return opt[sel].text;
}
// Converts number to Alpha equivalent
function getAlpha(num, base) {
	var alpha;
	switch(num % base) 
	{
		case 15:
			alpha = "F";
			break;
		case 14:
			alpha = "E";
			break;
		case 13:
			alpha = "D";
			break;
		case 12:
			alpha = "C";
			break;
		case 11:
			alpha = "B";
			break;
		case 10:
			alpha = "A";
			break;
	}
	return alpha;
}
function hideDrop() {
	document.getElementById("dropdown").style.visibility = "hidden";
	document.getElementById("fConD").style.visibility = "visible";
}
function hideFact() {
	document.getElementById("fConD").style.visibility = "hidden";
	document.getElementById("dropdown").style.visibility = "visible";
	document.getElementById("fCon").checked = false;
}
function showDrop() {
	document.getElementById("dropdown").style.visibility = "visible";
}
function seeOpt() {
    document.getElementById("fConD").style.visibility = "visible";
    document.getElementById("dropdown").style.visibility = "visible";
    document.getElementById("ans").innerHTML = "";
}
	